package canss.supports;

public final class basic
{
	public final static String IIf(String _strtest, String _strtrue, String _strfalse)
	{
		if(isEmpty(_strtest))
		{
			if(_strfalse==null)
				return "null";
			return _strfalse;
		}else
		{
			if(_strtrue==null)
				return "null";
			return _strtrue;	
		}
	}
	public final static String IIf(String _strtest, String _strfalse)
	{
		if(isEmpty(_strtest))
		{
			if(_strfalse==null)
				return "null";
			return _strfalse;
		}
		return _strtest;
	}
	public final static boolean isEmpty(String _str)
	{
		return _str == null || _str.trim().length() == 0 || _str.trim().equals("undefined");
	}

	public final static boolean isNotEmpty(String _str)
	{
		return !isEmpty(_str);
	}

	public final static boolean isEmpty(String _str0, String _str1)
	{
		return isEmpty(_str0) && isEmpty(_str1);
	}

	public final static boolean isEmpty(String _str0, String _str1, String _str2)
	{
		return isEmpty(_str0) && isEmpty(_str1) && isEmpty(_str2);
	}

	public final static boolean isNotEmpty(String _str0, String _str1)
	{
		return !isEmpty(_str0, _str1);
	}

	public final static boolean isNotEmpty(String _str0, String _str1, String _str2)
	{
		return !isEmpty(_str0, _str1, _str2);
	}
	public final static String padding(String _str, int totalwidth)
	{
		int maxlen = Math.max(_str.length(), totalwidth);
		StringBuffer buf = new StringBuffer(maxlen);
		buf.append(_str);
		for (int i = _str.length(); i < totalwidth; i++)
			buf.append(' ');
		return buf.toString();
	}
	public final static String padding(String _str, int totalwidth, String paddingChar)
	{
		int maxlen = Math.max(_str.length(), totalwidth);
		StringBuffer buf = new StringBuffer(maxlen);
		buf.append(_str);
		for (int i = _str.length(); i < totalwidth; i++)
			buf.append(paddingChar);
		return buf.toString();
	}
	public final static String padding(int totalwidth, String paddingChar)
	{
		StringBuffer buf = new StringBuffer(totalwidth);
		for (int i = 0; i < totalwidth; i++)
			buf.append(paddingChar);
		return buf.toString();
	}
	public final static String slicer(String _str, String _slicer, int length_per)
	{
		if(length_per<=0)
			throw new IllegalArgumentException("length_per is lower than zero");
		StringBuffer buf = new StringBuffer();
		for(int i=0; i<_str.length(); i+=length_per)
		{
			buf.append(_str.substring(i, Math.min(i+length_per, _str.length())));
			buf.append(_slicer);
		}
		return buf.toString();
	}

	public static final String var2str(Integer _int)
	{
		if (_int == null)
			return "null";
		else
			return _int.toString();
	}

	public static final String var2str(Double _dbl)
	{
		if (_dbl == null)
			return "null";
		else
			return _dbl.toString();
	}

	public static final String var2str(String _str)
	{
		if (_str == null)
			return "null";
		else
			return _str.toString();
	}

	public static final String filterString(String _str)
	{
		if (_str == null || _str.length() == 0)
			return _str;
		StringBuffer tmp = new StringBuffer(_str.length());
		for (int i = 0; i < _str.length(); i++)
		{
			char ch = _str.charAt(i);
			if (isalphadigit(ch))
				tmp.append(ch);
		}

		return tmp.toString();
	}

	public static final boolean isalphadigit(char _char)
	{
		return (_char >= 'a' && _char <= 'z') || (_char >= 'A' && _char <= 'Z') || (_char >= '0' && _char <= '9') || (_char == '_');
	}

	public static final String getXMLStart()
	{
		StringBuffer strbuf = new StringBuffer();

		strbuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strbuf.append("<canss:canss version=\"1.0\"");
		strbuf.append(" xmlns:canss=\"http://portal.canss.net/canss/ns#\"");
		strbuf.append(" xmlns:canssContent=\"http://portal.canss.net/canss/canssContent/ns#\"");
		strbuf.append(" xmlns:canssVideo=\"http://portal.canss.net/canss/canssVideo/ns#\"");
		strbuf.append(" xmlns:canssTV=\"http://portal.canss.net/canss/canssTV/ns#\"");
		strbuf.append(" xmlns:canssContentDescription=\"http://portal.canss.net/canss/canssContentDescription/ns#\"");
		strbuf.append(" xmlns:canssPerson=\"http://portal.canss.net/canss/canssPerson/ns#\"");
		strbuf.append(" xmlns:canssTrace=\"http://portal.canss.net/canss/canssTrace/ns#\"");
		strbuf.append(" xmlns:canssEvent=\"http://portal.canss.net/canss/canssEvent/ns#\"");
		strbuf.append(" xmlns:canssOrchestrator=\"http://portal.canss.net/canss/canssOrchestrator/ns#\"");
		strbuf.append(" xmlns:canssContract=\"http://portal.canss.net/canss/canssContract/ns#\"");
		strbuf.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
		strbuf.append(" xsi:schemaLocation=\"http://portal.canss.net/canss/ns# http://portal.canss.net/schema/canssschema.xsd\">");

		return strbuf.toString();
	}

	public static final String getXMLEnd()
	{
		StringBuffer strbuf = new StringBuffer();

		strbuf.append("</canss:canss>");

		return strbuf.toString();
	}

	public static final int PUBLISH_STATUS = 310;
	public static final int LOC_AGGREGATOR_1 = 100;
	public static final int LOC_AGGREGATOR_2 = 177;
	public static final int LOC_ENCODER_1 = 200;
	public static final int LOC_ENCODER_2 = 277;

	public static final int DEFAULT_PERSONTYPE_OPERATOR = 10;
	public static final int DEFAULT_PERSONTYPE_USER = 20;
	public static final int DEFAULT_PERSONTYPE_CP = 30;
	public static final int DEFAULT_PERSONTYPE_THIRDPARTY = 40;
	public static final int DEFAULT_BUZTYPE_THIRDPARTY_PERSONAL = 45;
	public static final int DEFAULT_BUZTYPE_THIRDPARTY_PUBLIC = 46;

	public static final String DEFAULT_PORTAL_PUBLICKEY = "C47A4BA90C5BB784CD2D007B60CCAC59";
	
	public static final String PORTAL_MODULE_ID = "120";
	
	public static final int LOG_PORTAL_USERJOIN = 391;
	public static final int LOG_PORTAL_USERLOGIN = 392;
	public static final int LOG_PORTAL_WEBPAGE_REQUEST = 393;
	public static final int LOG_PORTAL_CONTENTSEARCH_REQUEST = 394;
	public static final int LOG_PORTAL_CONTENTDETAIL_REQUEST = 395;
	
	//public static final String HOST_URL = "http://localhost:7001/CANSSWebService/";
	public static final String HOST_URL = "http://canss.net/CANSSWebService/";

	public static final String PERSONTYPEINTOSTR(int _type)
	{
		String tmp = null;
		switch (_type)
		{
		case DEFAULT_PERSONTYPE_OPERATOR:
			tmp = "OPERATOR";
			break;
		case DEFAULT_PERSONTYPE_USER:
			tmp = "USER";
			break;
		case DEFAULT_PERSONTYPE_CP:
			tmp = "CP";
			break;
		case DEFAULT_PERSONTYPE_THIRDPARTY:
			tmp = "THIRDPARTY";
			break;
		default:
			break;
		}
		return tmp;
	}
}
